/*
 * 转码任务的创建和查询
 */

var pageIndex = 0;
var pageSize = 10;
var pageCount = 0;

function createTask() {
	var transcodeModal = document.getElementsByClassName("transcode-modal")[0];
	transcodeModal.style.display = "";
}

function cancel() {
	document.querySelector("#create-videoId").value = "";
	document.querySelector("#create-transcodeTemplateId").value = "";
	document.querySelector("#create-transcodeTemplateFlag").value = "true";
	document.querySelector("#create-watermarkTemplateId").value = "";
	document.querySelector("#create-watermarkTemplateFlag").value = "true";
	document.querySelector("#create-callback").value = "";
	
	var transcodeModal = document.getElementsByClassName("transcode-modal")[0];
	transcodeModal.style.display = "none";
}

function confirm() {
	var videoId = document.querySelector("#create-videoId").value;
	var transcodeTemplateId = document.querySelector("#create-transcodeTemplateId").value;
	var transcodeTemplateFlag = document.querySelector("#create-transcodeTemplateFlag").value;
	var watermarkTemplateId = document.querySelector("#create-watermarkTemplateId").value;
	var watermarkTemplateFlag = document.querySelector("#create-watermarkTemplateFlag").value;
	var callback = document.querySelector("#create-callback").value;
	
	//要把字符串转成数字，因为后端接口接收的是数字类型数据
	transcodeTemplateId = Number(transcodeTemplateId);
	watermarkTemplateId = Number(watermarkTemplateId);
	
	var transcodeTemplates = [];
	transcodeTemplates.push(transcodeTemplateId);
	
	var params = {
		videoId: videoId,
		transcodeTemplates: transcodeTemplates,
		useDefaultTranscodeTemplate: transcodeTemplateFlag,
		watermarkTemplate: watermarkTemplateId,
		useDefaultWatermarkTemplate: watermarkTemplateFlag,
		callback: callback
	}
	
	ajax({
		url: "/task/transcode/create",
		type: "POST",
		contentType: "application/json",
		data: JSON.stringify(params),
		dataType: "json",
		success: function(response) {
			var res = JSON.parse(response);
			if(res.code == "0") {
				alert("转码任务创建成功");
				cancel();
				var params = {
			   		pageSize: pageSize,
			   		pageIndex: pageIndex
			   	}
			   	getTaskList(params);
			} else {
				alert(res.message);
			}
		},
		error: function(status) {
			
		}
	});
}

function search() {
	var params = {
		pageSize: pageSize,
		pageIndex: pageIndex
	}
	getTaskList(params);
}

function prePage() {
	if(pageIndex == 0) {
		return;
	}
	pageIndex = pageIndex - 1;
	var params = {
   		pageSize: pageSize,
   		pageIndex: pageIndex
   	}
   	getTaskList(params);
}

function nextPage() {
	if(pageIndex + 1 >= pageCount) {
		return;
	}
	pageIndex = pageIndex + 1;
	var params = {
   		pageSize: pageSize,
   		pageIndex: pageIndex
   	}
   	getTaskList(params);
}

function getTaskList(params) {
	ajax({
		url: "/task/search",
		type: "post",
		contentType: "application/json",
		data: JSON.stringify(params),
		dataType: "json",
		success: function(responseText) {
			var res = JSON.parse(responseText);
			console.log(res);
			if(res.code == "0") {
				var html = "";
				var list = res.data;
				pageCount = res.pageCount;
				for(var i = 0; i < list.length; i++) {
					var obj = list[i];
					var taskId = obj.taskId? obj.taskId : "";
					var videoId = obj.videoId? obj.videoId : "";
					var transcodeTemplateId = obj.transcodeTemplateId? obj.transcodeTemplateId : "";
					var watermarkId = obj.watermarkId? obj.watermarkId : "";
					var callback = obj.callback? obj.callback : "";
					var status = obj.status? obj.status : "";
					var time = new Date(obj.createTime);
					var createTime = yyyyMMddHHmmSS(time);
					html += "<tr><td>" + taskId + "</td><td>" + videoId + "</td><td>"
							+ transcodeTemplateId + "</td><td>" + watermarkId + "</td><td>"
							+ callback + "</td><td>" + status + "</td><td>"
							+ createTime + "</td></tr>";
				}
				var tbody = document.querySelector("#transcode-list");
				tbody.innerHTML = html;
				document.querySelector("#page-index").innerText = "第" + (pageIndex+1) + "/" + pageCount + "页";
			}
		},
		error: function(status) {
			
		}
	});
}

function yyyyMMddHHmmSS(date) {
	var year = date.getFullYear();
	var month = date.getMonth()+1 > 9 ? date.getMonth()+1 : "0"+(date.getMonth()+1);
	var day = date.getDate() > 9 ? date.getDate() : "0"+date.getDate();
	var hour = date.getHours() > 9 ? date.getHours() : "0"+date.getHours();
	var minute = date.getMinutes() > 9 ? date.getMinutes() : "0"+date.getMinutes();
	var second = date.getSeconds() > 9 ? date.getSeconds() : "0"+date.getSeconds();
	
	return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}